<template>
  <div id="student">
    <div class="background-picture">
    <!-- 顶部导航 -->
    <el-row class="padding-50">
      <el-col :span="24">
        <ul class="list">
          <li class="logo"><i class="iconfont icon-kaoshi"></i><span>DORO-Learning-Online</span></li>
          <li class="logo"><a href="javascript:;">我的课程</a></li>
          <li><a href="javascript:;" @click="exam()">我的考试</a></li>
          
          <li><router-link to="/student/message">我的消息</router-link></li>
          <li class="right" @mouseenter="flag = !flag" @mouseleave="flag = !flag">
            <a href="javascript:;"><i class="iconfont icon-Userselect icon"></i>{{ user.userName }}</a>
            <div class="msg" v-if="flag">
              <p @click="manage()">管理中心</p>
              <p class="exit" @click="exit()">退出</p>
            </div>
          </li>
        </ul>
      </el-col>
    </el-row>

    <!-- 加入的课程展示区域 -->
    <div id="myCourse">
      <div class="title">我加入的课程</div>
      <div class="wrapper">
        <ul class="top">
          <li class="order">课程列表</li>
          <li class="search-li">
            <div class="icon">
              <input type="text" placeholder="课程名称" class="search" v-model="key">
              <i class="el-icon-search"></i>
            </div>
          </li>
          <li><el-button type="primary" @click="searchCourses()">搜索课程</el-button></li>
        </ul>
        <ul class="paper" v-loading="loading">
          <li class="item" v-for="(item, index) in filteredCourses" :key="index" @click="toCourseDetail(item.id)">
            <div class="course-card">
              <h3 class="course-title">{{ item.courseName }}</h3>
              <p class="course-teacher">教师：{{ item.teacherName }}</p>
              <p class="course-desc">{{ item.description }}</p>
              <p class="course-id">课程 ID：{{ item.id }}</p>
            </div>
          </li>
        </ul>
      </div>
    </div>

    <!-- 欢迎页等其他模块 -->
    <div class="main">
      <h1>WELCOME</h1>
      <ul>
        <li><router-link to="/student/learn">具体课程</router-link></li>
      </ul>
      <router-view></router-view>
    </div>
</div>
    <v-footer></v-footer>
  </div>
</template>

<script>
import myFooter from "@/components/student/myFooter"
import { mapState } from "vuex"

export default {
  components: {
    "v-footer": myFooter
  },
  data() {
    return {
      flag: false,
      user: {},
      courseList: [],
      key: "",
      loading: false
    }
  },
  created() {
    this.userInfo()
    this.loadMyCourses()
  },
  computed: {
    ...mapState(["isPractice"]),
    filteredCourses() {
      if (!this.key) return this.courseList
      return this.courseList.filter(item =>
        item.courseName.toLowerCase().includes(this.key.toLowerCase()) ||
        item.teacherName.toLowerCase().includes(this.key.toLowerCase())
      )
    }
  },
  methods: {
    exit() {
      this.$router.push({ path: "/" })
      this.$cookies.remove("cname")
      this.$cookies.remove("cid")
    },
    manage() {
      this.$router.push({ path: "/student/manager" })
    },
    userInfo() {
      const studentName = this.$cookies.get("cname")
      const studentId = this.$cookies.get("cid")
      this.user.userName = studentName
      this.user.studentId = studentId
    },
    exam() {
      this.$router.push({ path: "/student/exam" })
    },
    message(){
      this.$router.push({ path: "/student/message" })
    },
    loadMyCourses() {
      this.loading = true
      const studentId = this.$cookies.get("cid")

      this.$axios.get(`/api/course/studentid`, { params: { studentID: studentId } })
        .then(res => {
          console.log("后端响应：", res)
          if (Array.isArray(res.data)) {
            this.courseList = res.data
          } else {
            this.$message.error("返回课程数据格式不正确")
          }
        })
        .catch(err => {
          console.error("请求出错：", err)
          this.$message.error("请求出错")
        })
        .finally(() => {
          this.loading = false
        })
    },
    searchCourses() {
      this.$message.success("已按课程名或教师名过滤")
    },
    toCourseDetail(courseId) {
      this.$router.push({ path: `/student/learn/${courseId}` })
    },
    AI() {
      this.$router.push("/student/AIhelper")
    }
  }
}
</script>

<style scoped>
/* 顶部导航样式保持不变 */
#student .padding-50 {
  margin: 0 auto;
  padding: 0 50px;
  box-shadow: 0 0 10px 4px rgba(1, 149, 255, 0.1);
  background-color: #fff;
}
.list a {
  text-decoration: none;
  color: #334046;
}
li {
  list-style: none;
  height: 60px;
  line-height: 60px;
}
#student .list {
  display: flex;
}
#student .list li {
  padding: 0 20px;
  cursor: pointer;
}
#student .list li:hover {
  background-color: #0195ff;
  transition: all 2s ease;
}
#student .list li:hover a {
  color: #fff;
}
#student .list .right {
  margin-left: auto;
  position: relative;
}
#student .list li.right :hover a {
  color: #000;
}
#student .list .logo {
  display: flex;
  font-weight: bold;
  color: #2f6c9f;
}
#student .list .logo i {
  font-size: 50px;
}
.right .msg {
  text-align: center;
  position: absolute;
  top: 60px;
  left: 0px;
  display: flex;
  flex-direction: column;
  border-radius: 2px;
  border-bottom: 3px solid #0195ff;
  background-color: #fff;
}
.right .msg p {
  height: 40px;
  line-height: 40px;
  width: 105px;
}
.right .msg p:hover {
  background-color: #0195ff;
}

/* 课程区域样式 */
#myCourse {
  width: 1150px;
  margin: 0 auto;
}
#myCourse .title {
  margin: 20px;
  font-size: 20px;
  font-weight: bold;
  color: #333;
}
#myCourse .wrapper {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
}

/* 顶部搜索栏 */
.wrapper .top {
  border-bottom: 1px solid #eee;
  margin-bottom: 20px;
  display: flex;
  flex-wrap: wrap;
}
.wrapper .top li {
  margin: 10px 20px;
  display: flex;
  align-items: center;
}
.top .search-li {
  margin-left: auto;
}
.top .search {
  padding: 10px;
  border-radius: 4px;
  border: 1px solid #eee;
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
  transition: border-color 0.3s ease-in-out, box-shadow 0.3s ease-in-out;
}
.top .search:hover {
  border-color: #0195ff;
}
.top .el-icon-search {
  position: absolute;
  right: 10px;
  top: 10px;
}
.top .icon {
  position: relative;
}
.wrapper .top .order {
  cursor: pointer;
}
.wrapper .top .order:hover {
  color: #0195ff;
  border-bottom: 2px solid #0195ff;
}

/* 卡片展示区域 */
.wrapper .paper {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
}

/* 每张课程卡片 */
.paper .item {
  width: 280px;
  min-height: 180px;
  padding: 16px 20px;
  margin: 15px;
  border-radius: 12px;
  background-color: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  cursor: pointer;
  transition: all 0.3s ease;
  overflow: hidden;
}
.paper .item:hover {
  transform: translateY(-4px);
  background-color: #0195ff;
  color: #fff;
  box-shadow: 0 8px 16px rgba(0, 149, 255, 0.3);
}

/* 鼠标悬浮时，卡片内容文字也变成白色 */
.paper .item:hover .course-title,
.paper .item:hover .course-teacher,
.paper .item:hover .course-desc,
.paper .item:hover .course-id {
  color: #fff;
}

/* 卡片内部字段 */
.course-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.course-title {
  font-size: 18px;
  font-weight: 600;
  color: #1a1a1a;
  margin: 0;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.course-teacher,
.course-id {
  font-size: 13px;
  color: #666;
  margin: 0;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.course-desc {
  font-size: 13px;
  color: #666;
  line-height: 1.4;
  max-height: 2.8em;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

</style>
